<?php
abstract class Itc_Controller_Action extends Zend_Controller_Action
{
	protected $title;

	protected $request;

	protected $router;

	/**
	 * Init controller
	 */
	public function init()
	{
		ini_set('max_execution_time', '100');

		$this->request		= $this->getRequest();
		$this->router		= $this->getFrontController()->getRouter();
		$this->title		= '';
	}

	/**
	 * Add head meta info
	 * @param sting $key
	 * @param string $value
	 */
	protected function addHeadMeta($key, $value)
	{
		$this->view->getHelper('HeadMeta')->headMeta($value, $key);
	}

	/**
	 * Set page title
	 * @param unknown_type $title
	 */
	protected function setTitle($title)
	{
		$this->title = $title;

		return $this;
	}
	
	/**
	 * Add page title
	 * @param string $title
	 */
	protected function addTitle($title, $def = ' - ')
	{
		$this->view->getHelper('HeadTitle')->headTitle(
			$title . $def,
			Zend_View_Helper_Placeholder_Container_Abstract::PREPEND
		);
	}

	/**
	 * Redirects to url presented by $urlOptions parameter
	 *
	 * @param $urlOptions assoc array of url parameters
	 * @param $name string router name
	 * @return void
	 */
	protected function redirector($urlOptions, $name = null)
	{
		$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
		$redirector->gotoRoute($urlOptions, $name);
	}
	
	/**
	 * Redirects everywhere (for example to index page)
	 *
	 */
	protected function redirectTo($url)
	{
		$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
		$redirector->gotoUrl($url);
	}

	/**
	 * Add css file to view
	 * @param string $css (/css/filename.css)
	 * @return Controller
	 */
	public function addCss($css)
	{
		$this->view->getHelper('HeadLink')->appendStylesheet($css);

		return $this;
	}
	
	/**
	 * Add js file to view
	 * @param string $js ('/js/filename.js)
	 * @return Controller
	 */
	public function addJs($js)
	{
		$this->view->getHelper('HeadScriptProxy')->appendFile($js);

		return $this;
	}
	
	/**
	 * Set js file to view
	 * @param string $js ('/js/filename.js)
	 * @return Controller
	 */
	public function setJs($js)
	{
		$this->view->getHelper('HeadScriptProxy')->setFile($js);

		return $this;
	}
	
	/**
	 * 
	 */
	protected function switchContext($action_contexts){
		$contextSwitch = $this->_helper->getHelper('contextSwitch');
        $contextSwitch->addActionContexts($action_contexts)->initContext();
	}
	
	/**
	 * 
	 */
	protected function autoSwitchContext($contexts,$force_context = false){
		$action = $this->request->getActionName();
		$contextSwitch = $this->_helper->getHelper('contextSwitch');
        $contextSwitch->addActionContext($action,$contexts);
		if($force_context){
        	$force_context = (string)$force_context;
        	$contextSwitch->initContext($force_context);
        }else{
        	$contextSwitch->initContext();
        }
	}
}